package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/system/service"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"boy-go/pkg/response"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"strings"
)

// AddSysLogininforRouter 添加路由
func AddSysLogininforRouter(group *gin.RouterGroup) {
	group.GET("/logininfor/:id", middlewares.HasPermission("monitor:logininfor:query"), get_logininfor)
	group.GET("/logininfor/list", middlewares.HasPermission("monitor:logininfor:list"), list_logininfor)
	group.POST("/logininfor", middlewares.HasPermission("monitor:logininfor:add"), middlewares.Logger("登录日志", middlewares.INSERT), add_logininfor)
	group.PUT("/logininfor", middlewares.HasPermission("monitor:logininfor:query"), middlewares.Logger("登录日志", middlewares.UPDATE), edit_logininfor)
	group.DELETE("/logininfor/:ids", middlewares.HasPermission("monitor:logininfor:remove"), middlewares.Logger("登录日志", middlewares.DELETE), del_logininfor)
	group.DELETE("/logininfor/clean", middlewares.HasPermission("monitor:logininfor:remove"), middlewares.Logger("登录日志", middlewares.CLEAN), clean_logininfor)
}

// get_logininfor
// @Summary 获取系统访问记录详情
// @Description 获取系统访问记录详情（管理页面）
// @Tags system-logininfor
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.SysLogininforModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/logininfor/{id} [get]
func get_logininfor(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewSysLogininforService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_logininfor
// @Summary 获取系统访问记录列表
// @Description 获取系统访问记录列表（管理页面）
// @Tags system-logininfor
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.SysLogininforModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/logininfor/list [get]
func list_logininfor(ctx *gin.Context) {
	req := vo.QuerySysLogininforReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	page, err := service.NewSysLogininforService().List(&req)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// add_logininfor
// @Summary 新增系统访问记录
// @Description 新增系统访问记录（管理页面）
// @Tags system-logininfor
// @Accept json
// @Produce json
// @Param req body  vo.AddSysLogininforReq true "req"
// @Success 200 {object} response.R[vo.AddSysLogininforRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/logininfor [post]
func add_logininfor(ctx *gin.Context) {
	var req vo.AddSysLogininforReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysLogininforService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_logininfor
// @Summary 编辑系统访问记录
// @Description 编辑系统访问记录（管理页面）
// @Tags system-logininfor
// @Accept json
// @Produce json
// @Param req body  vo.EditSysLogininforReq true "req"
// @Success 200 {object} response.R[vo.EditSysLogininforRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/logininfor [put]
func edit_logininfor(ctx *gin.Context) {
	var req vo.EditSysLogininforReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysLogininforService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_logininfor
// @Summary 删除系统访问记录
// @Description 删除系统访问记录（管理页面）
// @Tags system-logininfor
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/logininfor [delete]
func del_logininfor(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewSysLogininforService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}

func clean_logininfor(ctx *gin.Context) {
	err := service.NewSysLogininforService().Clean()
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, nil)
}
